package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbContentCategoryMapper;
import com.dongdong.mapper.TbContentMapper;
import com.dongdong.model.TbContent;
import com.dongdong.model.TbContentCategory;
import com.dongdong.model.TbContentCategoryExample;
import com.dongdong.model.TbContentExample;
import com.dongdong.service.ContentService;
import com.dongdong.utils.PageBean;
import com.dongdong.utils.PageResult;
import com.dongdong.utils.RedisKey;
import com.dongdong.utils.Result;
import com.dongdong.vo.ContentCategoryVO;
import com.dongdong.vo.ContentVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {


    @Autowired
    private TbContentCategoryMapper categoryMapper;

    @Autowired
    private TbContentMapper contentMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public PageResult<ContentCategoryVO> getContentCategoryPage(PageBean<ContentCategoryVO> pageBean) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(pageBean.getParams().getName())) {
            criteria.andNameLike("%" + pageBean.getParams().getName() + "%");
        }

        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<TbContentCategory> categoryList = categoryMapper.selectByExample(example);
        PageInfo<TbContentCategory> info = new PageInfo<>(categoryList);
        List<ContentCategoryVO> voList = categoryList.stream().map((x) -> {
            ContentCategoryVO categoryVO = new ContentCategoryVO();
            BeanUtils.copyProperties(x, categoryVO);
            return categoryVO;
        }).collect(Collectors.toList());


        return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), voList);
    }

    @Override
    public PageResult<ContentVO> getContentPage(PageBean<ContentVO> pageBean) {

        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<TbContent> contentList = contentMapper.selectByExample(null);
        PageInfo<TbContent> info = new PageInfo<>(contentList);
        List<ContentVO> voList = contentList.stream().map((x) -> {
            ContentVO vo = new ContentVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());


        return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), voList);
    }

    @Override
    public ContentCategoryVO getContent(Long id) {

        ContentCategoryVO ccvo = (ContentCategoryVO) redisTemplate.boundHashOps("dongdongshop:content").get(id);
        if (ccvo!=null){
            return ccvo;
        }

        TbContentCategory category = categoryMapper.selectByPrimaryKey(id);
        ContentCategoryVO categoryVO = new ContentCategoryVO();
        BeanUtils.copyProperties(category, categoryVO);


        List<TbContent> contentList = contentMapper.selectByExample(null);
        List<ContentVO> contentVOS = contentList.stream().map((x) -> {
            ContentVO vo = new ContentVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());

        Map<Long, List<ContentVO>> contentMap = contentVOS.stream().collect(Collectors.groupingBy((x) -> {
            return x.getCategoryId();
        }));


        categoryVO.setContentVOS(contentMap.get(categoryVO.getId()));
        redisTemplate.boundHashOps("dongdongshop:content").put(categoryVO.getId(), categoryVO);
        redisTemplate.boundHashOps(RedisKey.CONTENT_KEY).expire(RedisKey.EXPIRE_TIME.ordinal(), TimeUnit.MINUTES);
        return categoryVO;
    }

    @Override
    public ContentCategoryVO getCategoryById(Long id) {
        TbContentCategory category = categoryMapper.selectByPrimaryKey(id);
        ContentCategoryVO categoryVO = new ContentCategoryVO();
        BeanUtils.copyProperties(category,categoryVO);
        return categoryVO;
    }

    @Override
    public void updateCategory(ContentCategoryVO categoryVO) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(categoryVO,category);
        categoryMapper.updateByPrimaryKeySelective(category);
        redisTemplate.delete(RedisKey.CONTENT_KEY);
    }

    @Override
    public void addCategory(ContentCategoryVO categoryVO) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(categoryVO,category);
        categoryMapper.insertSelective(category);
        redisTemplate.delete(RedisKey.CONTENT_KEY);
    }

    @Override
    public List<ContentCategoryVO> getCategoryList() {
        List<TbContentCategory> categoryList = categoryMapper.selectByExample(null);
        List<ContentCategoryVO> voList = categoryList.stream().map((x) -> {
            ContentCategoryVO vo = new ContentCategoryVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public Result deleteCategoryById(Long id) {
        TbContentExample example = new TbContentExample();
        example.createCriteria().andCategoryIdEqualTo(id);
        List<TbContent> contentList = contentMapper.selectByExample(example);
        if (contentList != null || contentList.size() > 0){
            return Result.FAIL();
        }
        contentMapper.deleteByPrimaryKey(id);
        redisTemplate.delete(RedisKey.CONTENT_KEY);
        return Result.OK();
    }

}
